package utils;

import java.util.Vector;

/**
 * @author duncan
 */
public class KeyList
{
    private Vector list = new Vector();

    /**
     * KeyList constructor.
     */
    public KeyList()
    {
	
    }

    /**
     * Adds a new key with the specified value to the list.
     *
     * @param key The name of the key. This will be used to access the key.
     * @param value The value to give to the key.
     */
    public void addKey(String key, Object value)
    {
	list.addElement(new Key(key, value));
    }

    /**
     * Removes the specified key.
     *
     * @param key The name of the key to remove.
     */
    public void removeKey(String key)
    {
	for (int i = 0; i < list.size(); i++)
	{
	    if (((Key)list.get(i)).key.equals(key))
	    {
		list.remove(i);
		return;
	    }
	}
    }

    /**
     * Change the value of a key.
     *
     * @param key Key of the value to change.
     */
    public void changeKey(String key, Object value)
    {
	for (int i = 0; i < list.size(); i++)
	{
	    if (((Key)list.get(i)).key.equals(key))
		((Key)list.get(i)).value = value;
	}
    }

    /**
     * Returns the value of the specified key.
     *
     * @param key The name of the key. This will be used to access the key.
     * @return An Object with the specified Key.
     */
    public Object valueOf(String key)
    {
	for (int i = 0; i < list.size(); i++)
	{
	    if (((Key)list.get(i)).key.equals(key))
		return ((Key)list.get(i)).value;
	}
	return null;
    }

    /**
     * Returns the value as a String if possible, else returns an empty String.
     *
     * @param key The name of the key. This will be used to access the key.
     * @return A String with the specified Key.
     */
    public String requestString(String key)
    {
	for (int i = 0; i < list.size(); i++)
	{
	    if (((Key)list.get(i)).key.equals(key))
	    {
		try
		{
		    return (String) (((Key)list.get(i)).value + "");
		}
		catch (Exception e)
		{
		    return "";
		}
	    }
		
	}
	return "";
    }
}
